package com.zh.rent.comm;


import com.zh.rent.pojo.MailContent;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.MailContentService;
import com.zh.rent.service.StanAccountService;
import com.zh.rent.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Service
public class AsyncTaskService {

    @Autowired
    MailContentService MailContentServerImpl;
    @Autowired
    AccountService AccountServiceImpl;
    @Autowired
    StanAccountService StanAccountServiceImpl;

    @Async
    public int executeAsyncTask(int i) {
        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务：" + i);
        return i+6;
    }


    @Async
    public Future<String> doTaskTwo(String MailboxName,String Authorization,String Host,Integer accid,String accName,String accType) throws Exception {
        System.out.println("线程" + Thread.currentThread().getName()+"名字："+MailboxName+",密码："+Authorization);
        long start = System.currentTimeMillis();
        List<MailContent> list = new ArrayList<>();
        System.out.println("获取获取22222222");
        List<Map<String, Object>> emali = EmailUtil.Emali(MailboxName, Authorization, Host);
        System.out.println("获取获取33333333");
        if (emali != null) {
            for (Map<String, Object> strMap : emali) {
                MailContent mailContent = new MailContent();
                mailContent.setAccountid(accid);
                mailContent.setAccountName(accName);
                mailContent.setAccountType(accType);
                mailContent.setMailboxName(MailboxName);
                mailContent.setMailTime((String) strMap.get("Ttime"));
                mailContent.setMaliText((String) strMap.get("txtcontent"));
                mailContent.setSite((String) strMap.get("site"));
                mailContent.setMalicode((String) strMap.get("code"));
                list.add(mailContent);
            }
        }
        System.out.println("获取获取11111");
        if(list!=null){
            int i = MailContentServerImpl.updateMail(list);//添加进数据库
        }
        long end = System.currentTimeMillis();
        System.out.println("完成任务一，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("获取邮件任务完成");
    }
}
